package com.example.wanandroid.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.widget.ViewPager2;

import com.example.wanandroid.R;
import com.example.wanandroid.core.bean.BaseResponse;
import com.example.wanandroid.core.bean.navigation.NavigationListData;
import com.example.wanandroid.core.http.api.Apis;
import com.example.wanandroid.core.utils.HttpUtil;
import com.example.wanandroid.ui.adapter.NavCategoryItemAdapter;
import com.example.wanandroid.ui.adapter.NavCategoryPagerAdapter;
import com.zhengsr.tablib.bean.TabConfig;
import com.zhengsr.tablib.view.flow.TabFlowLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author jiangyi
 */
public class NavigationTabsFragment extends BaseFragment{
    String TAG = "NavigationTabsFragment";
    TabFlowLayout tabFlowLayout;
    ViewPager2 navCategoryPager;
    NavCategoryPagerAdapter navCategoryPagerAdapter;
    FragmentManager fragmentManager;
    List<Fragment> fragmentList = new ArrayList<>();
    View fragmentView;
    Retrofit retrofit;
    Apis apis;
    List<NavigationListData> navListData = new ArrayList<>();
    List<NavigationListData> filteredDataList = new ArrayList<>();
    @Override
    protected void showLoading() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentView = inflater.inflate(R.layout.nav_tabs_fragment,container,false);
        
        initFragment();
        if(filteredDataList.size()==0) {
            getData();
        }
        return fragmentView;
    }

    private void initFragment(){
        tabFlowLayout = fragmentView.findViewById(R.id.tab_flow);
        navCategoryPager = fragmentView.findViewById(R.id.nav_list_pager);
        fragmentManager = getActivity().getSupportFragmentManager();
        fragmentList = new ArrayList<>();
        navCategoryPagerAdapter = new NavCategoryPagerAdapter(getChildFragmentManager(),getLifecycle());
        navCategoryPager.setAdapter(navCategoryPagerAdapter);
        tabFlowLayout.setTabConfig(new TabConfig.Builder().setTextId(R.id.item_text).build());

    }

    private void getData(){
        apis = HttpUtil.getApi();

        Call<BaseResponse<List<NavigationListData>>> navListDataCall = apis.getNavigationListData();
        navListDataCall.enqueue(new Callback<BaseResponse<List<NavigationListData>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<NavigationListData>>> call, Response<BaseResponse<List<NavigationListData>>> response) {
                navListData = response.body().getData();
                filteredDataList = new ArrayList<>();
                for (int i = 0; i < navListData.size(); i++) {
                    if(navListData.get(i).getArticles().size()==0) {
                        continue;
                    }
                    filteredDataList.add(navListData.get(i));
                    NavigationListFragment fragment = NavigationListFragment.newInstance(navListData.get(i), i);
                    fragmentList.add(fragment);
                }
                navCategoryPagerAdapter.setFragmentList(fragmentList);
                tabFlowLayout.setAdapter(new NavCategoryItemAdapter(R.layout.textview_item, filteredDataList,getContext()){
                    @Override
                    public void onItemClick(View view, NavigationListData data, int position) {
                        super.onItemClick(view, data, position);
//                        navCategoryPager.setCurrentItem(position,false);
                        navCategoryPager.post(()->{
                            navCategoryPager.setCurrentItem(position,false);
                            navCategoryPager.requestTransform();
                        });
                    }
                });
                navCategoryPager.registerOnPageChangeCallback(changeCallback);

            }

            @Override
            public void onFailure(Call<BaseResponse<List<NavigationListData>>> call, Throwable t) {

            }
        });
    }

    private ViewPager2.OnPageChangeCallback changeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            //可以来设置选中时tab的大小
            tabFlowLayout.setItemClickByOutside(position);
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        tabFlowLayout = null;
        navCategoryPager = null;
    }
}
